<template>
    <el-dialog v-model="dialogVisible" :title="titleText" width="416" style="height: 471px; ">
        <el-form label-width="auto" style="width: 390px" :model="tagItem">
            <el-form-item label="角色名称" style="margin-top: 20px;">
                <el-input placeholder="请输入" v-model="tagItem.name" />
            </el-form-item>
            <el-form-item label="角色介绍">
                <el-input v-model="tagItem.intro" style="width: 390px" :autosize="{ minRows: 6, maxRows: 6 }"
                    type="textarea" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="创建时间">
                {{ getTime }}
            </el-form-item>
            <el-form-item label="创建人">
            </el-form-item>

        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="onConfirmBtn()">
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script setup>
import { computed, ref } from 'vue';
import { addRoleApi, updateRoleApi } from '../../../api/role';
import { useGlobalStore } from '../../../store/global';

const dialogVisible = ref(false);
const tagItem = ref({});
const getTime = computed(() => {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
    return `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`;
})
const doctor_token = localStorage.getItem('doctor_token');
const emit = defineEmits(['findTag']);
const store = useGlobalStore();
const creatorId = computed(() => store.doctorInfo._id);
const titleText = ref('');

const openDialog = (formItem) => {
    if (formItem) {
        tagItem.value = { ...formItem };
        titleText.value = '编辑角色信息'
    } else {
        tagItem.value = {};
        titleText.value = '新增角色'
    }

    dialogVisible.value = true;
};
defineExpose({
    openDialog
});



//添加
function onAddBtn() {
    addRoleApi({ ...tagItem.value, creatorId: creatorId.value }).then(res => {
        if (res.code == 200) {
            ElMessage.success('添加成功!');
            emit('findTag');
        } else {
            ElMessage.error(res.message);
        }
    })
}

//修改
function onUpdatedBtn() {
    updateRoleApi({ ...tagItem.value }).then(res => {
        if (res.code == 200) {
            ElMessage.success('修改成功!');
            emit('findTag');
        } else {
            ElMessage.error(res.message);
        }
    })
}


function onConfirmBtn() {
    if (tagItem.value._id) {
        onUpdatedBtn();
    } else {
        onAddBtn();
    };
    dialogVisible.value = false;
    emit('findTag');
}





</script>

<style scoped>
.dialog-footer {
    margin-top: 10px;
}
</style>
